/**
 * @author nttdocomo
 */
define(function(require) {
	require("./model");
	aries.klass("aries.selection.DataViewModel", aries.selection.Model.extend({

		bindComponent : function(view) {
			var me = this, eventListeners = {
				'refresh' : me.refresh
			};

			me.view = view;
			me.bindStore(view.getStore());

			eventListeners[view.triggerEvent] = me.onItemClick;
			eventListeners[view.triggerCtEvent] = me.onContainerClick;
			
			$.each(eventListeners,function(eventName,fn){
				view.on(eventName,fn,me);
			})

			if (me.enableKeyNav) {
				me.initKeyNav(view);
			}
		},

		onItemClick : function(view, record, item, index, e) {
			this.selectWithEvent(record, e);
		},

		// Allow the DataView to update the ui
		onSelectChange : function(record, isSelected, suppressEvent, commitFn) {
			var me = this, view = me.view, eventName = isSelected ? 'select' : 'deselect';

			if ((suppressEvent || me.trigger('before' + eventName, me, record)) !== false && commitFn() !== false) {

				if (view) {
					if (isSelected) {
						view.onItemSelect(record);
					} else {
						view.onItemDeselect(record);
					}
				}

				if (!suppressEvent) {
					me.trigger(eventName, me, record);
				}
			}
		}
	}))
})
